package com.ruoyi.ai.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ai.domain.VectorDbResource;
import com.ruoyi.ai.service.IVectorDbResourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 向量数据库资源Controller
 * 
 * @author ruoyi
 * @date 2025-07-02
 */
@RestController
@RequestMapping("/ai/vectorDb")
public class VectorDbResourceController extends BaseController
{
    @Autowired
    private IVectorDbResourceService vectorDbResourceService;

    /**
     * 查询向量数据库资源列表
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:list')")
    @GetMapping("/list")
    public TableDataInfo list(VectorDbResource vectorDbResource)
    {
        startPage();
        List<VectorDbResource> list = vectorDbResourceService.selectVectorDbResourceList(vectorDbResource);
        return getDataTable(list);
    }

    /**
     * 导出向量数据库资源列表
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:export')")
    @Log(title = "向量数据库资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VectorDbResource vectorDbResource)
    {
        List<VectorDbResource> list = vectorDbResourceService.selectVectorDbResourceList(vectorDbResource);
        ExcelUtil<VectorDbResource> util = new ExcelUtil<VectorDbResource>(VectorDbResource.class);
        util.exportExcel(response, list, "向量数据库资源数据");
    }

    /**
     * 获取向量数据库资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vectorDbResourceService.selectVectorDbResourceById(id));
    }

    /**
     * 新增向量数据库资源
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:add')")
    @Log(title = "向量数据库资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VectorDbResource vectorDbResource)
    {
        return toAjax(vectorDbResourceService.insertVectorDbResource(vectorDbResource));
    }

    /**
     * 修改向量数据库资源
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:edit')")
    @Log(title = "向量数据库资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VectorDbResource vectorDbResource)
    {
        return toAjax(vectorDbResourceService.updateVectorDbResource(vectorDbResource));
    }

    /**
     * 删除向量数据库资源
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:remove')")
    @Log(title = "向量数据库资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vectorDbResourceService.deleteVectorDbResourceByIds(ids));
    }

    /**
     * 测试向量数据库连接
     */
    @PreAuthorize("@ss.hasPermi('ai:vectorDatabase:test')")
    @Log(title = "向量数据库连接测试", businessType = BusinessType.OTHER)
    @PostMapping("/test/{id}")
    public AjaxResult testConnection(@PathVariable Long id)
    {
        boolean result = vectorDbResourceService.testConnection(id);
        if (result) {
            return success("连接测试成功");
        } else {
            return error("连接测试失败");
        }
    }
}